package com.cqh.recruit.controller.school;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqh.recruit.pojo.dto.ActivityQueryDto;
import com.cqh.recruit.pojo.entity.Activity;
import com.cqh.recruit.pojo.vo.ActivityListVo;
import com.cqh.recruit.result.Result;
import com.cqh.recruit.service.IActivityService;
import com.cqh.recruit.task.ActivityStatusUpdateTask;
import com.cqh.recruit.utils.EmployeeThreadLocal;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 *
 *  前端控制器
 *
 *
 * @author dh
 * @since 2025-10-18
 * @version v1.0
 */
@RestController("schoolActivityController")
@RequestMapping("/api/school/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    @Autowired
    private ActivityStatusUpdateTask activityStatusUpdateTask;


    @GetMapping("/find")
    @Operation(summary = "根据id查询活动")
    public Result find(Long id) {
        if (id == null) {
            return Result.buildFail(2001, "id不能为空");
        }
        ActivityListVo activity = activityService.findActivityById(id);
        return Result.buildSuccess(activity);

    }

    @PostMapping("/add")
    @Operation(summary = "添加活动")
    public Result add(@RequestBody Activity  activity) {
        Long employeeId = EmployeeThreadLocal.get().getId();
        activity.setEmployeeId(employeeId);
        activity.setCreateTime(LocalDateTime.now());
        activityService.save(activity);
        return Result.buildSuccess();
    }
    @PostMapping("/update")
    @Operation(summary = "修改活动")
    public Result update(@RequestBody Activity activity) {
        activity.setUpdateTime(LocalDateTime.now());
        activityService.updateById(activity);
        return Result.buildSuccess();
    }
    @GetMapping("/delete")
    @Operation(summary = "删除活动")
    public Result delete(Long id) {
        if (id == null) {
            return Result.buildFail(2001, "id不能为空");
        }
        activityService.removeById(id);
        return Result.buildSuccess();
    }
    @PostMapping("/list")
    @Operation(summary = "查询活动列表")
    public Result list(@RequestBody ActivityQueryDto activityQueryDto) {
        Page<ActivityListVo> resultPage = activityService.findActivityByQueryDto(activityQueryDto);
        return Result.buildSuccess(resultPage.getRecords(), resultPage.getTotal());
    }

    @PostMapping("/updateStatus")
    @Operation(summary = "一键更新活动状态")
    public Result updateStatus() {
        try {
            int updateCount = activityStatusUpdateTask.updateActivityStatus();
            return Result.buildSuccess("活动状态更新成功，共更新 " + updateCount + " 个活动");
        } catch (Exception e) {
            return Result.buildFail(5001, "活动状态更新失败：" + e.getMessage());
        }
    }


}

